/**
 * File              : main.rs
 * Author            : leoatchina <leoatchina@outlook.com>
 * Date              : 2020.12.05
 * Last Modified Date: 2020.12.05
 * Last Modified By  : leoatchina <leoatchina@outlook.com>
 */

//1、Option是标准库定义的1个枚举，形式：
//enum Option<T> {
//  Some(T),
//  None,
//}


//2、使用方式
fn plus_one(x: Option<i32>) -> Option<i32> {
    match x {
        Some(x) => Some(x+1),
        None => None,
    }
}

fn main() {
    let some_number  = Some(5);
    let some_string  = Some(String::from("a string"));
    let some_number2 = Some(6);
    let some_string2 = Some(String::from("a second string"));

    let absent_number: Option<i32> = None;

    let x: i32         = 5;
    let y: Option<i32> = Some(5);
    let z: Option<i32> = Some(6);

    let mut temp = 0;
    match y {
        //  judge if i is NONE
        Some(i) => { temp = i; }
        None => {println!("do nothing");}
    }

    match z {
        Some(j) => { temp = j + 1; }
        None => {println!("nothing to do");}
    }

    let sum = x + temp;
    println!("sum = {}", sum);


    if let Some(value) = plus_one(y) {
        println!("value = {}", value);
    } else {
        println!("do nothing");
    }

    if let Some(value) = plus_one(absent_number) {
        println!("value = {}", value);
    } else {
        println!("nothing to do with NONE");
    }

    println!("Hello, world!");
}

